
// 加载函数
$().ready(function () {
    Channel_opening();
    PwdChange();
    GetRandom();
    NameChange();
    CodeChange();
    RegisterButtonClick();
    PhoneChange();
});


// 是否可以注册状态
function Channel_opening()
{
    $("#check-box").click(function(){
        if($(this).is(':checked'))
        {
            $(this).parent().siblings("button").removeClass().addClass("Register-button-one").val(2);
        }
        else
        {
            $(this).parent().siblings("button").removeClass().addClass("Register-button-two").val(1);

        }
    });
}

// 获取验证码
function GetRandom() {
    $("#Code-button").click(function () {
        var number = Math.floor(Math.random() * 9000 + 999);
        $(this).html(number);
        $("#Code-button").click(function () {
            if ($(this).text() == "点击获取") {
                var number = Math.floor(Math.random() * 9000 + 999);
                $(this).html(number);
                console.log(number);
            }
        });
    });
}

// 监听验证码不能输入超过四个数字
function CodeChange()
{
    $("#txtUserCode").bind("input propertychange",function(){
        var word=$(this).val().trim();
        if(word.length>4)
        {
            word=word.substring(0,4);
        }
        if(word.length==0)
        {
            $(this).siblings("label").css("visibility", "visible");
            if ($(this).siblings().html() != "请输入验证码") { $("#Code-Error-message").html("请输入验证码"); }
        }
        else
        {
            $(this).siblings("label").css("visibility","hidden");
        }
        $(this).val(word);
    });
}



//监听手机输入框
function PhoneChange()
{
    $("#txtUserPhone").bind("input propertychange", function () {
        if ($(this).val() != "") {
            $(this).siblings().css("visibility", "hidden");
        }
        else {
            $(this).siblings().css("visibility", "visible").html("还没告诉我你的手机号");
        }
    });
}
// 监听用户名输入框
function NameChange()
{
    $("#txtUserID").bind("input propertychange",function(){
        if($(this).val()!="")
        {
            $(this).siblings().css("visibility","hidden");
        }
        else
        {
            $(this).siblings().css("visibility","visible");
        }
    });
}


//注册状态判定
function RegisterButtonClick(){
    $("#RegisterButton").click(function(){
        if ($(this).val() == 2) {
            if ($("#Code-button").html() == "点击获取") {
                window.alert('请先获取验证码');
                return;
            }
            if ($("#txtUserCode").val() == "") { $("#Code-Error-message").html("请输入正确的验证码").css("visibility", "visible"); return; }

            if ($("#Code-button").html() != $("#txtUserCode").val()) {
                $("#Code-Error-message").html("验证码有误").css("visibility", "visible");
            }
        }
        else {
            var count = 0;
            if ($("#Code-button").html() == "点击获取") {
                window.alert('请先获取验证码');
                count++;
            }
            else if ($("#txtUserCode").val() == "") {
                $("#Code-Error-message").html("请输入正确的验证码").css("visibility", "visible");
                count++;
            }
            else if ($("#Code-button").html() != $("#txtUserCode").val()) {
                $("#Code-Error-message").html("验证码有误").css("visibility", "visible");
                count++;
            }

            if ($("#txtUserPhone").val() == "") {
                $("#txtUserPhone").siblings().css("visibility", "visible").html("还没告诉我你的手机号");
                count++;
            }
            if ($("#txtUserPWD").val() == "") {
                count++;
                $("#txtUserPWD").siblings().css("visibility", "visible").html("密码不能低于六位");
            }
            if ($("#txtUserID").val() == "") {
                count++;
                $("#txtUserID").siblings().css("visibility", "visible").html("让我知道你的名字吧");
            }

            console.log(count);
            if (count == 0) {
                console.log(0);
                $.ajax({

                    url: 'Register.ashx',
                    data: { UserPhone: $("#txtUserPhone").val(), UserName: $("#txtUserID").val(), UserPwd: $("#txtUserPWD").val() },
                    type: 'POST',
                    success: function (data) {
                        console.log(data);
                        switch (data) {
                            case "1":
                                alert("此手机号已存在");

                                $("#txtUserPhone").val("").focus();
                                break;
                            case "2":
                                alert("注册成功！欢迎加入凌风大家庭！");
                                location.href = "";
                                break;
                            case "3":
                                alert("不明原因，请联系管理员");
                                break;
                        }
                    }
                });
            }
        }
    });
}

// 密码判断状态
function PwdChange()
{
    $("#txtUserPWD").bind("input propertychange",function(){
        $(".security-color-one").css("visibility","visible");
        $("#security strong").css("visibility","visible");
        $("#pwd-Error-message").css("visibility", "visible");
        $("#security label").css({"visibility":"visible","color":"red"});
        $("#security label").html("弱");
        var word=$(this).val();
        var Strx=word.split('');
        if(word.length<6)
        {
            $(this).siblings("label").css("display", "inline").html("密码不能低于六位");
        }
        else
        {
            $(this).siblings("label").css("display","none");
            var count1=0;
            var count2=0;
            var count3=0;
            Strx.forEach(function(item){
                var number=item.charCodeAt();
                if(number>=48&&number<=57)
                {
                    count1=1;
                }
                else if((number>=65&&number<=90)||(number>=97&&number<=122))
                {
                    count2=1;
                }
                else 
                {
                    count3=1;
                }
            });
            var Sum=count1+count2+count3;
            switch(Sum)
            {
                case 1:
                    $(".security-color-two").css("visibility","hidden");
                    $(".security-color-there").css("visibility","hidden")
                    $("#security label").css({"visibility":"visible","color":"red"});
                    $("#security label").val("弱");
                    break;
                case 2:
                    $(".security-color-two").css("visibility","visible");
                    $(".security-color-there").css("visibility","hidden");
                    $("#security label").css({"visibility":"visible","color":"rgb(172, 172, 3)"});
                    $("#security label").text("中");
                    break;
                case 3:
                    $(".security-color-two").css("visibility","visible");
                    $(".security-color-there").css("visibility","visible");
                    $("#security label").css({"visibility":"visible","color":"greenyellow"});
                    $("#security label").text("强");
                    break;
            }
        }
    });
}